//
//  ViewController.m
//  protobuf-test
//
//  Created by CoderXu on 2017/5/15.
//  Copyright © 2017年 XanderXu. All rights reserved.
//

#import "ViewController.h"
#import "Person.pbobjc.h"
#import "DataTransTool.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    person *personModel = [[person alloc] init];
    personModel.name = @"xu";
    personModel.icon = @"123.jpg";
    personModel.level = 6;
    
    
    NSData *data = personModel.data;
    NSLog(@"%@--%@--%zd--%zd",personModel,data,data.length,personModel.serializedSize);
    
    NSString *resultString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",resultString);

    //测试的序列化
    NSError *error;
    person *personModel2 = [[person alloc] initWithData:data error:&error];
    NSLog(@"%@--%@--%@",personModel2,personModel2.description,error);
    //测试的序列化2
    person *personModel3 = [person parseFromData:data error:&error];
    NSLog(@"%@--%@--%@",personModel3,personModel3.description,error);
    
}

-(void) socketTrans {
    person *personModel = [[person alloc] init];
    personModel.name = @"xu";
    personModel.icon = @"123.jpg";
    personModel.level = 6;
    
    NSOutputStream *opStram = [[NSOutputStream alloc] initToMemory];
    [DataTransTool writeDelimitedTo:personModel output:opStram];
    NSData *data = [opStram propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
    //NSData *data = personModel.data;
    NSLog(@"%@--%@--%zd--%zd",personModel,data,data.length,personModel.serializedSize);
    
    NSString *resultString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",resultString);
    
    int headLength = [DataTransTool computeRawVarint32Size:data.length];
    NSData *headData = [data subdataWithRange:NSMakeRange(0, headLength)];
    int length = 0;
    [headData getBytes:&length length:headLength];
    
    NSData *delimitData = [data subdataWithRange:NSMakeRange(data.length -length, length)];
    //测试的序列化
    NSError *error;
    person *personModel2 = [[person alloc] initWithData:data error:&error];
    NSLog(@"%@--%@--%@",personModel2,personModel2.description,error);
    //测试的序列化2
    person *personModel3 = [person parseFromData:delimitData error:&error];
    NSLog(@"%@--%@--%@",personModel3,personModel3.description,error);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
